/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginPanel.java
 *
 * Created on Aug 26, 2010, 7:23:14 PM
 */
package org.apache.directory.http.client.ui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.directory.http.client.ui.callback.LoginHandler;


/**
 *
 * @author kayyagari
 */
public class LoginPanel extends javax.swing.JPanel implements ActionListener
{

    private LoginHandler loginHandler;


    /** Creates new form LoginPanel */
    public LoginPanel()
    {
        initComponents();

        txtUserName.setText( "uid=admin,ou=system" );
        txtPassword.setText( "secret" );
        
        btnLogin.addActionListener( this );
        txtPassword.addActionListener( this );
    }


    public void actionPerformed( ActionEvent e )
    {
        if ( getServiceUrl() == null )
        {
            txtUrl.requestFocus();
        }
        else
        {
            login();
        }

    }


    private void login()
    {
        if ( loginHandler != null )
        {
            loginHandler.login( txtUserName.getText(), txtPassword.getPassword() );
        }

    }


    public URL getServiceUrl()
    {

        try
        {
            return new URL( txtUrl.getText().trim() );
        }
        catch ( MalformedURLException e )
        {
            return null;
        }
    }


    public void clearPassword()
    {
        txtPassword.setText( "" );
    }


    public void setLoginHandler( LoginHandler loginHandler )
    {
        this.loginHandler = loginHandler;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        lblPassword = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        lblUrl = new javax.swing.JLabel();
        txtUrl = new javax.swing.JTextField();

        lblPassword.setText( "Password:" );

        lblUserName.setText( "User Name:" );

        btnLogin.setText( "Login" );

        jLabel1.setText( "e.x uid=admin,ou=system" );

        lblUrl.setText( "ServiceURL:" );

        txtUrl.setText( "http://localhost:7009/adminapp/ldap" );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout( this );
        this.setLayout( layout );
        layout.setHorizontalGroup( layout.createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING )
            .add(
                layout
                    .createSequentialGroup()
                    .add(
                        layout
                            .createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING )
                            .add( layout.createSequentialGroup().add( 177, 177, 177 ).add( jLabel1 ) )
                            .add( layout.createSequentialGroup().add( 204, 204, 204 ).add( btnLogin ) )
                            .add(
                                layout
                                    .createSequentialGroup()
                                    .add(
                                        layout
                                            .createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING, false )
                                            .add(
                                                org.jdesktop.layout.GroupLayout.TRAILING,
                                                layout
                                                    .createSequentialGroup()
                                                    .addContainerGap( org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                        Short.MAX_VALUE ).add( lblPassword ).add( 1, 1, 1 ) )
                                            .add(
                                                layout
                                                    .createSequentialGroup()
                                                    .add( 96, 96, 96 )
                                                    .add(
                                                        layout
                                                            .createParallelGroup(
                                                                org.jdesktop.layout.GroupLayout.LEADING )
                                                            .add( lblUrl,
                                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73,
                                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE )
                                                            .add( lblUserName ) ) ) )
                                    .add( 1, 1, 1 )
                                    .add(
                                        layout
                                            .createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING )
                                            .add( txtUrl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 269,
                                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE )
                                            .add(
                                                layout
                                                    .createParallelGroup( org.jdesktop.layout.GroupLayout.TRAILING,
                                                        false )
                                                    .add( org.jdesktop.layout.GroupLayout.LEADING, txtPassword )
                                                    .add( org.jdesktop.layout.GroupLayout.LEADING, txtUserName,
                                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 206,
                                                        Short.MAX_VALUE ) ) ) ) )
                    .addContainerGap( 122, Short.MAX_VALUE ) ) );
        layout.setVerticalGroup( layout.createParallelGroup( org.jdesktop.layout.GroupLayout.LEADING ).add(
            layout
                .createSequentialGroup()
                .add( 31, 31, 31 )
                .add(
                    layout
                        .createParallelGroup( org.jdesktop.layout.GroupLayout.BASELINE )
                        .add( txtUrl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                            org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                            org.jdesktop.layout.GroupLayout.PREFERRED_SIZE ).add( lblUrl ) )
                .addPreferredGap( org.jdesktop.layout.LayoutStyle.RELATED )
                .add(
                    layout
                        .createParallelGroup( org.jdesktop.layout.GroupLayout.BASELINE )
                        .add( lblUserName )
                        .add( txtUserName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                            org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                            org.jdesktop.layout.GroupLayout.PREFERRED_SIZE ) )
                .add( 2, 2, 2 )
                .add( jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12,
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE )
                .addPreferredGap( org.jdesktop.layout.LayoutStyle.RELATED )
                .add(
                    layout
                        .createParallelGroup( org.jdesktop.layout.GroupLayout.BASELINE )
                        .add( txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                            org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                            org.jdesktop.layout.GroupLayout.PREFERRED_SIZE ).add( lblPassword ) )
                .addPreferredGap( org.jdesktop.layout.LayoutStyle.RELATED ).add( btnLogin )
                .addContainerGap( 64, Short.MAX_VALUE ) ) );
    }// </editor-fold>//GEN-END:initComponents
     // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUrl;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUrl;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
